package br.com.academix.shell.view
{
	import br.com.academix.commons.PipeAwareModuleConstants;
	import br.com.academix.shell.PureMVCSampleConstants;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.pipes.interfaces.IPipeAware;
	import org.puremvc.as3.multicore.utilities.pipes.interfaces.IPipeFitting;
	import org.puremvc.as3.multicore.utilities.pipes.interfaces.IPipeMessage;
	import org.puremvc.as3.multicore.utilities.pipes.messages.Message;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.Junction;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.JunctionMediator;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.Pipe;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.TeeMerge;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.TeeSplit;

	public class ShellJunctionMediator extends JunctionMediator
	{
		public static const NAME:String = "ShellJunctionMediator";
		
		public function ShellJunctionMediator()
		{
			super(NAME, new Junction());
		}
		
		override public function onRegister():void{
			junction.registerPipe(PipeAwareModuleConstants.SHELL_TO_MODULE_PIPE, Junction.OUTPUT, new TeeSplit());
			junction.registerPipe(PipeAwareModuleConstants.MODULE_TO_SHELL_PIPE, Junction.INPUT, new TeeMerge());
			junction.addPipeListener(PipeAwareModuleConstants.MODULE_TO_SHELL_PIPE, this, handlePipeMessage);
		}
		
		override public function listNotificationInterests():Array{
			return[PureMVCSampleConstants.MODULE_ADDED,
				   PipeAwareModuleConstants.SEND_MESSAGE_MODULE,
				   PipeAwareModuleConstants.REMOVE_MODULE,
				   PureMVCSampleConstants.MODULE_REMOVED];
		}
		
		override public function handleNotification(notification:INotification):void{
			switch(notification.getName()){
				case PureMVCSampleConstants.MODULE_ADDED: 
					connectModule(notification.getBody() as IPipeAware);
		 		break;
		 		case PureMVCSampleConstants.MODULE_REMOVED:
                     var shellOutFitting:TeeSplit = junction.retrievePipe( PipeAwareModuleConstants.SHELL_TO_MODULE_PIPE ) as TeeSplit;
                    shellOutFitting.disconnect(); 
                    trace("111111111111111111111111111111111");
                   /*  junction.removePipe( PipeAwareModuleConstants.MODULE_TO_SHELL_PIPE );
					junction.removePipe( PipeAwareModuleConstants.SHELL_TO_MODULE_PIPE ); */
                    //junction.removePipe(PipeAwareModuleConstants.SHELL_TO_MODULE_PIPE);
                break;
		 		case PipeAwareModuleConstants.SEND_MESSAGE_MODULE: 
		 			junction.sendMessage(PipeAwareModuleConstants.SHELL_TO_MODULE_PIPE, new Message(PipeAwareModuleConstants.SHELL_TO_MODULE_MESSAGE, PipeAwareModuleConstants.SEND_MESSAGE_MODULE));
		 		break;
		 		case PipeAwareModuleConstants.REMOVE_MODULE:
		 			junction.sendMessage(PipeAwareModuleConstants.SHELL_TO_MODULE_PIPE, new Message(PipeAwareModuleConstants.SHELL_TO_MODULE_MESSAGE, PipeAwareModuleConstants.REMOVE_MODULE));
		 		break;
				default: break;
			}
		}
		
		override public function handlePipeMessage(message:IPipeMessage):void{
		 	var header:String = Message(message).getHeader() as String;
		 	switch(header){
		 		case PipeAwareModuleConstants.SEND_MESSAGE_SHELL: 
		 			sendNotification(PipeAwareModuleConstants.SEND_MESSAGE_SHELL);
		 		break;
		 		default:break;
		 	}
		}
		
		public function connectModule( module:IPipeAware ):void
        {            
            var moduleToShell:IPipeFitting = new Pipe();
            module.acceptOutputPipe( PipeAwareModuleConstants.MODULE_TO_SHELL_PIPE, moduleToShell );
            
            var shellInFitting:TeeMerge = junction.retrievePipe( PipeAwareModuleConstants.MODULE_TO_SHELL_PIPE ) as TeeMerge;
            shellInFitting.connectInput( moduleToShell );
            
            var shellToModule:IPipeFitting = new Pipe();
            module.acceptInputPipe( PipeAwareModuleConstants.SHELL_TO_MODULE_PIPE, shellToModule );
        
            var shellOutFitting:TeeSplit = junction.retrievePipe( PipeAwareModuleConstants.SHELL_TO_MODULE_PIPE ) as TeeSplit;
            shellOutFitting.connect( shellToModule );
        }
		
	}
}